class Solution {
public:
    int minFallingPathSum(vector<vector<int>>& matrix)
    {
        int m = matrix.size();
        int n = matrix[0].size();
        vector<vector<int>> dp(m, vector<int>(n + 1, INT_MAX));
        for (int i = 1; i <= n; i++)
        {
            dp[0][i] = matrix[0][i - 1];
            dp[i - 1].push_back(INT_MAX);
        }
        for (int i = 1; i < m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                dp[i][j] = min({ dp[i - 1][j - 1], dp[i - 1][j], dp[i - 1][j + 1] }) + matrix[i][j - 1];
            }
        }
        return *min_element(dp[m - 1].begin() + 1, dp[m - 1].end() - 1);
    }
};